package tests;

import static org.junit.Assert.*;

import org.junit.Test;

	import static org.junit.Assert.*;

	import java.util.List;

	import org.junit.After;
	import org.junit.AfterClass;
	import org.junit.Before;
	import org.junit.BeforeClass;
	import org.junit.Test;

	import classes.Korisnik;
	import dao.DAOFactory;
	import dao.KorisnikDAO;

	public class KorisnikDAOTest {
		private static KorisnikDAO kdao;
		
		@BeforeClass
		public static void fixtureSetUp() throws Exception{
			DAOFactory.connect();
		}
		
		@AfterClass
		public static void fixtureTearDown() throws Exception{
			DAOFactory.disconnect();
		}
		
		private static Korisnik k;
		
		@Before
		public void setUp() throws Exception{
			kdao = DAOFactory.getKorisnikDAO();
			k = new Korisnik();
			k.setIme("Testni");
			k.setAdresa("tess");
			k.setTelefon("aaaa");
			k.setPrezime("Korisnicic");
			k.setUsername("korisnikovic");
			k.setPassword("password");
			k.setId(100);
			kdao.insertKorisnik(k);
			}
		
		@After
		public void tearDown() throws Exception{
			kdao.deleteKorisnik(k.getId());
		}
		//radi sa podacima iz moje baze(potrebno imati neke korisnike unutar)
		@Test
		public void testInsertKorisnik() throws Exception {
			Korisnik k=new Korisnik();
			k.setIme("am");
			k.setPassword("adsssadsa");
			k.setAdresa("lppp");
			k.setPrezime("k");
			k.setUsername("ooss");
			k.setTelefon("435");
			kdao.insertKorisnik(k);
			assertEquals(kdao.selectKorisnik(k.getUsername()).getIme(), k.getIme());
		}
		//radi sa podacima iz moje baze(potrebno imati neke korisnike unutar)

		@Test
		public void testSelectKorisnik() throws Exception {
			assertEquals(kdao.selectKorisnik(k.getUsername()).getUsername(), "korisnikovic");
		}
	@Test
		public void testUpdateKorisnik() throws Exception {	
		//radi sa podacima iz moje baze(potrebno imati neke korisnike unutar)
			Korisnik receiver = kdao.selectKorisnik("am");
			receiver.setUsername("lol");
			kdao.updateKorisnik(receiver);
			assertEquals("lol", receiver.getUsername());
		}

	/*	@Test
		public void testSelectSviKorisnici() throws Exception {
			List<Korisnik> korisnici = kdao.selectSviKorisnici();
			assertEquals(korisnici.size(),1);
		}
		
		@Test
		public void testSelectSviKorisniciNemaNikog() throws Exception{
			kdao.deleteKorisnik(k.getId());
			
			List<Korisnik> korisnici = kdao.selectSviKorisnici();
			assertEquals(0, korisnici.size());
		}
		
		@Test(expected=Exception.class)
		public void testSelectJedanKorisnikNemaGa() throws Exception{
			kdao.deleteKorisnik(k.getId());
			
			@SuppressWarnings("unused")
			Korisnik k1 = kdao.selectKorisnik(k.getUsername());
			
		}
		
		@Test
		public void testUpdateNepostojecegKorisnika() throws Exception{
			kdao.deleteKorisnik(k.getId());
			
			int eval = kdao.updateKorisnik(k);
			assertEquals(0,eval);
		}
		
		@Test
		public void testDeleteNepostojecegKorisnika() throws Exception{
			kdao.deleteKorisnik(k.getId());
			
			int eval = kdao.deleteKorisnik(k.getId());
			assertEquals(0,eval);
		}
		
		@Test
		public void testExistsDoesExist(){
			assertEquals(k.getId(), kdao.exists(k.getUsername(), k.getPassword()));
		}
		
		@Test
		public void testExistsDoesNotExist(){
			assertEquals(-1, kdao.exists("lolz", "trololo"));
		}*/
		
}
